package com.example.minefieldkeepercloud.activity.ipreport;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.fragment.IpSettingFragment;
import com.example.minefieldkeepercloud.fragment.ManyIpSettingFragment;
import com.example.minefieldkeepercloud.util.TitleControl;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.dialog.LoadingDialog;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyFragmentPagerAdapter;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Ip助手
 */
public class IPReportActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;

    @BindView(R.id.viewpager)
    ViewPager viewpager;

    //IP设置
    @BindView(R.id.tv_ipsetting)
    TextView tv_ipsetting;
    //批量设置
    @BindView(R.id.tv_allsetting)
    TextView tv_allsetting;



    private static final int NUM = 2;//设置Viewpager 里面的 fragment的数量

    public int nowNUm=0;//当前的fragment序号

    public int getNowNUm() {
        return nowNUm;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ipreport);
        ButterKnife.bind(this);
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle(getString(R.string.IPreportname));
        loadingDialog=new LoadingDialog(this,2);
        setBottomLabState(0);
        viewpager.setAdapter(new CustomLazyFragmentPagerAdapter(getSupportFragmentManager()));
    }

    /**
     * 点击的底部分类图标状态
     *
     * @param position
     */
    private void setBottomLabState(int position) {
        switch (position) {
            case 0:
                setTextAndImg(tv_ipsetting, true);
                setTextAndImg(tv_allsetting,  false);
                break;
            case 1:
                setTextAndImg(tv_ipsetting, false);
                setTextAndImg(tv_allsetting,  true);
                break;
        }
    }
    /**
     * 设置选择分类的样式
     * @param isBlue
     */
    private void setTextAndImg(TextView tv, boolean isBlue) {

        if (isBlue) {
            tv.setTextColor(ContextCompat.getColor(this, R.color.system_color));
        } else {
            tv.setTextColor(ContextCompat.getColor(this, R.color.clor_maingray));
        }
    }


    private static class CustomLazyFragmentPagerAdapter extends LazyFragmentPagerAdapter {

        private CustomLazyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(ViewGroup container, int position) {
            if (position == 0) {
                return  new IpSettingFragment();
            } else if (position == 1) {
                return  new ManyIpSettingFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return NUM;
        }

    }


    /**
     * 底部类别点击事件处理
     *
     * @param view
     */
    @OnClick({R.id.tv_ipsetting, R.id.tv_allsetting})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ipsetting://ip设置
                nowNUm=0;
                setBottomLabState(0);
                viewpager.setCurrentItem(0, false);
                break;
            case R.id.tv_allsetting://批量设置
                nowNUm=1;
                setBottomLabState(1);
                viewpager.setCurrentItem(1, false);
                break;


        }
    }
}
